package com.jucai.pppeiyouclient.base;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by Kuky on 2017-02-22.
 */

public abstract class CommonRecyclerViewAdapter<T> extends RecyclerView.Adapter<CommonRecyclerViewAdapter.CommonHolder> {
    private static final int HEADER = 1001;
    private static final int BOTTOM = 1002;

    protected List<T> mData;
    protected Context mContext;
    protected LayoutInflater mInflater;
    protected OnItemClickListener mOnItemClickListener;
    protected View mHeaderView, mBottomView;

    public List<T> getList() {
        return mData;
    }

    ;

    /**
     * 设置头部
     *
     * @param headerView
     */
    public void setHeader(View headerView) {
        this.mHeaderView = headerView;
        notifyItemInserted(0);
    }

    /**
     * 设置底部
     *
     * @param bottom
     */
    public void setBottom(View bottom) {
        this.mBottomView = bottom;
        if (haveHeader()) {
            notifyItemInserted(mData.size() + 1);
        } else {
            notifyItemInserted(mData.size());
        }
    }

    /**
     * 监听接口
     */
    public interface OnItemClickListener {
        void onItemClickListener(View view, int position);

        void onItemLongClickListener(View view, int position);
    }

    /**
     * 设置监听
     *
     * @param onItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    /**
     * 设置数据
     *
     * @param data
     */
    public void setData(List<T> data) {
        this.mData = data;
        notifyDataSetChanged();
    }

    public CommonRecyclerViewAdapter(Context context, List<T> data) {
        this.mContext = context;
        this.mData = data;
        this.mInflater = LayoutInflater.from(context);
    }

    /**
     * 创建holder
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public CommonHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CommonHolder holder = null;
        if (viewType == BOTTOM && mBottomView != null) {
            holder = new CommonHolder(mBottomView);
        } else if (viewType == HEADER && mHeaderView != null) {
            holder = new CommonHolder(mHeaderView);
        } else {
            holder = CommonHolder.getHolder(mContext, parent, getLayoutId(viewType));
        }
        return holder;
    }

    /**
     * 绑定数据
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(final CommonHolder holder, int position) {
        if (!isHeaderView(position) && !isBottom(position)) {
            if (haveHeader()) position--;

            //具体子类自行实现
            bindHolder(holder, mData.get(position));
            //监听
            if (mOnItemClickListener != null) {
                holder.mConvertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mOnItemClickListener.onItemClickListener(view, getRealPos(holder));
                    }
                });

                holder.mConvertView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {
                        mOnItemClickListener.onItemLongClickListener(view, getRealPos(holder));
                        return true;
                    }
                });
            }
        }
    }

    /**
     * 子类具体实现自身的数据绑定
     *
     * @param holder
     * @param t
     */
    public abstract void bindHolder(CommonHolder holder, T t);

    /**
     * 根据位置获取viewType
     *
     * @param position
     * @return
     */

    public int getItemViewType(int position) {
        if (isHeaderView(position)) {
            return HEADER;
        } else if (isBottom(position)) {
            return BOTTOM;
        } else {
            return getItemType(position);
        }
    }

    /**
     * 子类具体返回itemType
     *
     * @param position
     * @return
     */
    protected abstract int getItemType(int position);

    /**
     * 根据viewType获取layoutId
     *
     * @param viewType
     * @return
     */
    public abstract int getLayoutId(int viewType);

    /**
     * 数据数量
     *
     * @return
     */
    @Override
    public int getItemCount() {
        int count = (mData == null ? 0 : mData.size());

        if (mHeaderView != null) {
            count++;
        }
        if (mBottomView != null) {
            count++;
        }
        return count;
    }

    /**
     * 是否存在头部
     *
     * @return
     */
    protected boolean haveHeader() {
        return mHeaderView != null;
    }

    /**
     * 是否存在尾部
     *
     * @return
     */
    protected boolean haveBottom() {
        return mBottomView != null;
    }

    /**
     * 当前是否头部
     *
     * @param pos
     * @return
     */
    protected boolean isHeaderView(int pos) {
        return haveHeader() && pos == 0;
    }

    /**
     * 当前是否尾部
     *
     * @param pos
     * @return
     */
    protected boolean isBottom(int pos) {
        return haveBottom() && pos == getItemCount() - 1;
    }

    /**
     * 获取实际位置，避免动态增删影响
     *
     * @param holder
     * @return
     */
    public int getRealPos(CommonHolder holder) {
        return holder.getLayoutPosition();
    }


    public void removeData(T t) {
        mData.remove(t);
        notifyDataSetChanged();
    }

    /**
     * 通用ViewHolder
     */
    protected static class CommonHolder extends RecyclerView.ViewHolder {
        private SparseArray<View> mViews;
        private View mConvertView;
        private Context mContext;

        public CommonHolder(View itemView, Context context, ViewGroup parent) {
            super(itemView);
            mViews = new SparseArray<>();
            mContext = context;
            mConvertView = itemView;
        }

        public CommonHolder(View itemView) {
            super(itemView);
            mViews = new SparseArray<>();
            mConvertView = itemView;
        }

        /**
         * 获取holder
         *
         * @param context
         * @param parent
         * @param layoutId
         * @return
         */
        public static CommonHolder getHolder(Context context, ViewGroup parent, int layoutId) {
            View itemView = LayoutInflater.from(context).inflate(layoutId, parent, false);
            CommonHolder holder = new CommonHolder(itemView, context, parent);
            return holder;
        }

        /**
         * 获取view
         *
         * @param viewId
         * @param <T>
         * @return
         */
        public <T extends View> T getView(int viewId) {
            View view = mViews.get(viewId);
            if (view == null) {
                view = itemView.findViewById(viewId);
                mViews.put(viewId, view);
            }
            return (T) view;
        }

        /**
         * 设置文字
         *
         * @param viewId
         * @param text
         * @return
         */
        public CommonHolder setText(int viewId, String text) {
            TextView textView = getView(viewId);
            textView.setText(text);
            return this;
        }

        /**
         * 设置图片及其大小
         *
         * @param viewId
         * @param imageId
         * @return
         */
        public CommonHolder setImage(int viewId, int imageId, int width, int height) {
            ImageView imageView = getView(viewId);
            Glide.with(mContext)
                    .load(imageId)
                    .override(width, height)
                    .centerCrop()
                    .into(imageView);
            return this;
        }

        public CommonHolder setImage(int viewId, int imageId) {
            ImageView imageView = getView(viewId);
            Glide.with(mContext)
                    .load(imageId)
                    .centerCrop()
                    .into(imageView);
            return this;
        }


        public CommonHolder setImage(int viewId, String imageId, int width, int height) {
            ImageView imageView = getView(viewId);
            Glide.with(mContext)
                    .load(imageId)
                    .override(width, height)
                    .centerCrop()
                    .into(imageView);
            return this;
        }

        public CommonHolder setImage(int viewId, String imageId) {
            ImageView imageView = getView(viewId);
            Glide.with(mContext)
                    .load(imageId)
                    .centerCrop()
                    .into(imageView);
            return this;
        }


        /**
         * 设置字体颜色
         *
         * @param viewId
         * @param color
         * @return
         */
        public CommonHolder setTextColor(int viewId, int color) {
            TextView textView = getView(viewId);
            textView.setTextColor(color);
            return this;
        }

        /**
         * 设置背景
         *
         * @param viewId
         * @param resId
         * @return
         */
        public CommonHolder setBackground(int viewId, int resId) {
            View view = getView(viewId);
            view.setBackgroundResource(resId);
            return this;
        }


        /**
         * 设置Bitmap
         *
         * @param viewId
         * @param bitmap
         * @param width
         * @param height
         * @return
         */
        public CommonHolder setImageBitmap(int viewId, Bitmap bitmap, int width, int height) {
            ImageView imageView = getView(viewId);
            Glide.with(mContext)
                    .load(bitmap)
                    .asBitmap()
                    .into(imageView);
            return this;
        }

        /**
         * 设置隐藏
         *
         * @param viewId
         * @return
         */
        public CommonHolder setGone(int viewId) {
            View view = getView(viewId);
            view.setVisibility(View.GONE);
            return this;
        }

        /**
         * 设置可见
         *
         * @param viewId
         * @return
         */
        public CommonHolder setVisible(int viewId) {
            View view = getView(viewId);
            view.setVisibility(View.VISIBLE);
            return this;
        }

        /**
         * 设置不可见
         *
         * @param viewId
         * @return
         */
        public CommonHolder setInvisible(int viewId) {
            View view = getView(viewId);
            view.setVisibility(View.INVISIBLE);
            return this;
        }

        /**
         * 设置监听事件
         *
         * @param viewId
         * @param listener
         * @return
         */
        public CommonHolder setOnclickListener(int viewId, View.OnClickListener listener) {
            View view = getView(viewId);
            view.setOnClickListener(listener);
            return this;
        }
    }
}